Lagrange Multiplier

Overview

The Lagrange multiplier method solves a beautiful geometric problem: how do you find the maximum (or minimum) of a function when you’re constrained to move along a curve?

The key insight: At the optimal point, the contour lines of your objective function f must be tangent to the constraint curve — if they weren’t, you could slide along the constraint and improve f. Tangency means the gradients ∇f and ∇g point in the same direction, giving us the condition: ∇f = λ∇g.

In the interactive demo above, you can we maximize f(x,y) = xy subject to x + y = 1. Drag the white dot along the constraint line to find the optimum.

What you’re seeing: The purple curves are contours of f(x,y) = xy (lines where xy is constant). The green line is the constraint x + y = 1. As you drag the point, notice how ∇f (amber) and ∇g (green) rotate — they only become parallel at the optimum (x = y = 0.5), where λ = 0.5.

Mathematical Formulation

To solve the constrained problem, you form the Lagrangian:

L(x, y, λ) = f(x,y) − λ · g(x,y)

where g(x,y) = x + y − 1. Then set all partial derivatives to zero:

Equation Meaning
∂L/∂x = 0 → y = λ ∂f/∂x = λ · ∂g/∂x
∂L/∂y = 0 → x = λ ∂f/∂y = λ · ∂g/∂y
∂L/∂λ = 0 → x + y = 1 constraint is satisfied

From the first two equations: x = y. Substituting into the constraint: x = y = ½, and λ = ½. The maximum value is f(½, ½) = ¼.

Intuition for λ

The multiplier tells you how much the optimum would improve if you relaxed the constraint. Here, λ = 0.5 means that if the constraint were x + y = 1.001 instead, the maximum of xy would increase by roughly 0.5 × 0.001 = 0.0005. It’s the “shadow price” of the constraint.